package com.huang.day2;

import com.sun.org.apache.xpath.internal.operations.String;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Constructor;


/**
 * @author HuangShen
 * @Description
 * @create 2021-04-01 17:56
 */
public class Test2 {




    @Test
    public void main1() throws InterruptedException {


        Thread t1 = new Thread(() -> {
            try {
                Thread.sleep(100000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }, "t1");

        t1.start();

        Thread.sleep(1000);
        t1.interrupt();
    }

    static int r=0;
    /**
     * 同步案例
     */
    @Test
    public void main2() throws InterruptedException {


        Thread t1 = new Thread(() -> {
            try {
                Thread.sleep(1000);
                r=10;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t1");

        t1.start();
        t1.join();
        System.out.println(r);
    }

    /**
     *
     */
    @Test
    public void main3() throws InterruptedException {


        Thread t2 = new Thread(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t2");
        t2.start();
        Thread t1 = new Thread(() -> {
//            try {
//                wait(10000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            while (true){
//                if (!Thread.currentThread().isInterrupted()){
//                    System.out.println(Thread.currentThread().getName()+"被打断了");
//
//                    break;
//                }
//
//            }

            try {
                t2.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }, "t1");



        t1.start();
        Thread.sleep(1000);
        t1.interrupt();
        System.out.println(t1.isInterrupted());
    }


    /**
     * 两阶段终止 模式
     */
    @Test
    public void main4() throws InterruptedException, NoSuchMethodException {
        Thread thread1 = new Thread(() -> {
            while (true) {
                Thread thread = Thread.currentThread();
                if (thread.isInterrupted()) {
                    System.out.println("退出");
                    break;
                }
                try {
                    Thread.sleep(2000);
                    System.out.println("执行监控");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    thread.interrupt();
                }
            }

        }, "t1");
        thread1.start();

        Thread.sleep(8000);

        thread1.interrupt();

        Class<String> stringClass = String.class;
        Constructor<String> declaredConstructor = stringClass.getDeclaredConstructor();
        declaredConstructor.setAccessible(true);
        String string = new String();
        Class<? extends String> aClass = string.getClass();


    }
}
